package com.cn.learn.starter.util;

/**
 * @ClassName ZipUtil
 * @Description zip 文件解压工具类
 * @Author JiJiang
 * @Date 2023/7/7 15:10
 * @Version 1.0
 */


import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * zip解压示例
 *
 * @author : ydfind
 * @date : 2019-06-06
 */
//@Slf4j
public class ZipUtil {

    public static void main(String[] args) throws IOException {
        String filename = "D:\\JavaIOTest\\阿波尼亚泳装.zip";
        String path = "D:\\JavaIOTest\\unpack";
        ZipUtil.unZip(filename, path);
    }


    public static void unZip(String sourceFilename, String targetDir) throws IOException {
        unZip(new File(sourceFilename), targetDir);
    }

    /**
     * 将sourceFile解压到targetDir
     *
     * @param sourceFile
     * @param targetDir
     * @throws RuntimeException
     */
    public static void unZip(File sourceFile, String targetDir) throws IOException {
        long start = System.currentTimeMillis();

        //如果需要被解压的文件不存在
        if (!sourceFile.exists()) {
            throw new FileNotFoundException("cannot find the file = " + sourceFile.getPath());
        }
        ZipFile zipFile = null;
        try {
            //将文件对象用ZipFile包装
            zipFile = new ZipFile(sourceFile);

            //entries为解压文件中的元素
            Enumeration<?> entries = zipFile.entries();
            //如果解压文件中有元素
            while (entries.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) entries.nextElement();

                //如果当前的元素为目录
                if (entry.isDirectory()) {
                    String dirPath = targetDir + "/" + entry.getName();
                    createDirIfNotExist(dirPath);
                }
                //如果为非目录文件
                else {
                    File targetFile = new File(targetDir + "/" + entry.getName());
                    createFileIfNotExist(targetFile);
                    InputStream is = null;
                    FileOutputStream fos = null;
                    try {
                        is = zipFile.getInputStream(entry);
                        fos = new FileOutputStream(targetFile);
                        int len;
                        byte[] buf = new byte[1024];
                        while ((len = is.read(buf)) != -1) {
                            fos.write(buf, 0, len);
                        }
                    } finally {
                        try {
                            fos.close();
                        } catch (Exception e) {
//                            log.warn("close FileOutputStream exception", e);
                            System.out.println("close FileOutputStream exception:\r\n" + e.getMessage());
                        }
                        try {
                            is.close();
                        } catch (Exception e) {
//                            log.warn("close InputStream exception", e);
                            System.out.println("close InputStream exception:\r\n" + e.getMessage());


                        }
                    }
                }
            }
//            log.info("解压完成，耗时：" + (System.currentTimeMillis() - start) + " ms");
            System.out.println("解压完成，耗时：" + (System.currentTimeMillis() - start) + " ms");

        } finally {
            if (zipFile != null) {
                try {
                    zipFile.close();
                } catch (IOException e) {
//                    log.warn("close zipFile exception", e);
                    System.out.println("close zipFile exception:\r\n" + e.getMessage());

                }
            }
        }
    }

    public static void createDirIfNotExist(String path) {
        File file = new File(path);
        createDirIfNotExist(file);
    }

    public static void createDirIfNotExist(File file) {
        if (!file.exists()) {
            file.mkdirs();
        }
    }

    public static void createFileIfNotExist(File file) throws IOException {
        createParentDirIfNotExist(file);
        file.createNewFile();
    }

//    public static void createParentDirIfNotExist(String filename){
//        File file = new File(filename);
//        createParentDirIfNotExist(file);
//    }

    public static void createParentDirIfNotExist(File file) {
        createDirIfNotExist(file.getParentFile());
    }
}

